package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 套餐业务层
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        //创建一个setmeal对象接收setmealDTO中的相关数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //存入setmeal表
        setmealMapper.insert(setmeal);
        //存入list
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }

        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐分类查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());

        return pageResult;
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断ids是否为空
        if (ids == null || ids.size() == 0) {
            throw new DeletionNotAllowedException("未选择套餐");
        }

        //查询得到setmeal集合
        List<Setmeal> setmeals = setmealMapper.getByIds(ids);

        //遍历集合，查看是否有在售套餐
        for (Setmeal setmeal : setmeals) {
            //如果有，抛出异常
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //批量删除套餐
        setmealMapper.deleteBatch(ids);

        //删除套餐和菜品关系表中的数据
        setmealDishMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO = new SetmealVO();

        //在setmealMapper中查询得到setmeal
        Setmeal setmeal = setmealMapper.getById(id);
        //将setmeal拷贝到setmealVO中
        BeanUtils.copyProperties(setmeal, setmealVO);
        //查询setmealDish表得到套餐菜品关系数组并存入setmealVO
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishes);
        //返回setmealVO
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //用一个setmeal接收setmealDTO中的套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //用一个List集合接收setmealDTO中的套餐和菜品关系数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }

        //存入setmeal
        setmealMapper.update(setmeal);
        //删除原有套餐菜品关系数据
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //重新存入套餐菜品关系数据
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐起售或停售
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        //如果是要停售套餐，则直接停售
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        if (status == StatusConstant.DISABLE) {
            setmealMapper.update(setmeal);
        } else {
            //如果是要起售套餐，先查询套餐菜品表中的该套餐的所有菜品id集合
            List<Dish> dishes = dishMapper.getBySetmealId(id);
            //遍历菜品集合，查看是否有停售菜品
            if (dishes != null && dishes.size() != 0) {
                for (Dish dish : dishes) {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        //如果有，抛出异常
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }

            //起售套餐
            setmealMapper.update(setmeal);
        }

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
